package com.jiangjiang.eshop.pay.schedule;

import com.jiangjiang.eshop.pay.constant.PayTransactionStatus;
import com.jiangjiang.eshop.pay.dto.PayTransactionDTO;
import com.jiangjiang.eshop.pay.dto.PayTransactionQueryCondition;
import com.jiangjiang.eshop.pay.service.PayTransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Jianghaowen
 * @Date 2021/9/30
 * @Description: TODO
 * @Version V1.0
 **/
@Slf4j
@Component
public class UnpaiedTimeoutTask {

    @Autowired
    private PayTransactionService payTransactionService;

    /**
     * 执行任务
     */
    @Scheduled(fixedRate = 10 * 1000)
    public void execute(){
        try {
            PayTransactionQueryCondition condition = PayTransactionQueryCondition.builder()
                    .status(PayTransactionStatus.UNPAYED)
                    .build();
            List<PayTransactionDTO> payTransactionDTOList = payTransactionService.queryByCondition(condition);
            for (PayTransactionDTO payTransactionDTO : payTransactionDTOList) {
                if(System.currentTimeMillis() - payTransactionDTO.getGmtCreate().getTime() > 30 * 60 * 1000){
                    payTransactionDTO.setStatus(PayTransactionStatus.CLOSED);
                    payTransactionService.update(payTransactionDTO);
                }
            }
        }catch (Exception e){
            log.warn("UnpaiedTimeoutTask error",e);
        }
    }
}
